package Version2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class BootStrap {

    private int port=8080;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void startUp() throws IOException {

        //创建一个服务器Socket对象
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("=====>>>Minicat start on port：" + port);

        while (true){

            //接收一个请求后

            Socket socket = serverSocket.accept();
            System.out.println("接收一个请求后");

            /*
            * 建立连接后，并不能马上就能够获取对象，而是需要等待一段时间，才会收到请求
            *
            * 因为三次握手后，浏览器才会发起HTTP请求，后端才能够连接
            * */

            //我们这里需要读取请求信息
            InputStream inputStream = socket.getInputStream();

            //发现输入流中有数据
            int count=0;
            while (count==0){
                count=inputStream.available();
                System.out.println("循环中");
            }

            //把输入流中的数据，读取到byte[]中
            byte[] bytes=new byte[count];
            inputStream.read(bytes);

            System.out.println(new String(bytes));
        }

    }

    public static void main(String[] args) {
        BootStrap bootStrap = new BootStrap();

        try {
            bootStrap.startUp();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
